package com.hotel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.auto.JSONAuthentication;
import com.hotel.entity.HoHouse;
import com.hotel.entity.HoNotice;
import com.hotel.entity.HoUser;
import com.hotel.mapper.HoNoticeMapper;
import com.hotel.mapper.HoUserMapper;
import com.hotel.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/ho-notice")
public class HoNoticeController extends JSONAuthentication {
    @Autowired
    HoNoticeMapper hoNoticeMapper;
    @Autowired
    HoUserMapper hoUserMapper;

    @ApiOperation(value = "添加公告")
    @PostMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("title") String  title,@RequestParam("content") String  content ) throws Exception{
        HoNotice hoNotice=new HoNotice();
        hoNotice.setUserId(userId);
        hoNotice.setTitle(title);
        hoNotice.setContent(content);
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        hoNotice.setCreatTime(timestamp);
        if(hoNoticeMapper.insert(hoNotice)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除公告")
    @PostMapping("/del")
    public void delOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=hoNoticeMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改公告标题通过id")
    @PostMapping("/updTitle")
    public void delOrderasd(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ,@RequestParam("title") String title) throws Exception{
        HoNotice hoNotice = hoNoticeMapper.selectById(id);
        hoNotice.setTitle(title);
        if(hoNoticeMapper.updateById(hoNotice)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改公告内容通过id")
    @PostMapping("/updContent")
    public void delOrderasds(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ,@RequestParam("content") String content) throws Exception{
        HoNotice hoNotice = hoNoticeMapper.selectById(id);
        hoNotice.setTitle(content);
        if(hoNoticeMapper.updateById(hoNotice)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有公告")
    @PostMapping("/findAllNotice")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<HoNotice> shUserPage=new Page<>(current,size);
        Page<HoNotice> shUserPages=hoNoticeMapper.selectPage(shUserPage,null);
        List<HoNotice> list = shUserPages.getRecords();
        int total=hoNoticeMapper.selectCount(null);
        for(HoNotice a:list){
           a.setHoUser(hoUserMapper.selectById(a.getUserId()));
        }
        if(list.size()>0){
            Result ok = Result.ok().data("userList",list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据标题查找公告")
    @PostMapping("/findNoticeByTitle")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("title") String title)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("title",title);
        Page<HoNotice> shUserPage=new Page<>(current,size);
        Page<HoNotice> shUserPages=hoNoticeMapper.selectPage(shUserPage,wrapper);
        List<HoNotice> list = shUserPages.getRecords();
        int total=hoNoticeMapper.selectCount(null);
        for(HoNotice a:list){
            a.setHoUser(hoUserMapper.selectById(a.getUserId()));
        }
        if(list.size()>0){
            Result ok = Result.ok().data("userList",list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

